﻿package com.vicentediaz
{
    import com.asual.swfaddress.*;
    import com.myp.flow.*;
    import flash.events.*;

    public class SWFAddressProxy extends EventDispatcher
    {
        private var started:Boolean = false;
        private var internalChange:Boolean = false;
        public var maxLevels:uint = 3;
        public static var verbose:Boolean = true;
        private static var instance:SWFAddressProxy;

        public function SWFAddressProxy(param1:IEventDispatcher = null)
        {
            super(param1);
            return;
        }// end function

        private function say(... args) : void
        {
            if (verbose)
            {
                trace("<<< SWF ADDRESS PROXY : " + args + " >>>");
            }// end if
            return;
        }// end function

        private function onSWFAddressChange(param1:SWFAddressEvent) : void
        {
            this.say("ejecutando ruta");
            this.execute();
            return;
        }// end function

        public function start() : void
        {
            if (this.started)
            {
                return;
            }// end if
            SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, this.onSWFAddressChange, false, 0, true);
            Flow.addEventListener(FlowEvent.SOME_LEVEL_CHANGE, this.onFlowSomeChange, false, 0, true);
            this.started = true;
            return;
        }// end function

        public function getLevel(param1:uint) : String
        {
            var _loc_2:* = SWFAddress.getPath();
            var _loc_3:* = _loc_2.split("/");
            return _loc_3[param1];
        }// end function

        private function onFlowSomeChange(param1:FlowEvent) : void
        {
            var _loc_3:String;
            var _loc_4:String;
            var _loc_2:String;
            var _loc_5:uint;
            while (_loc_5++ < this.maxLevels)
            {
                // label
                _loc_3 = "level" + _loc_5;
                if (Flow.checkLevel(_loc_3))
                {
                    _loc_4 = Flow.level(_loc_3).currentSection;
                    if (_loc_4)
                    {
                        if (_loc_4 != "home" && _loc_4 != null)
                        {
                            _loc_2 = _loc_2 + (Flow.level(_loc_3).currentSection + "/");
                        }// end if
                    }// end if
                }// end if
            }// end while
            SWFAddress.setValue(_loc_2);
            this.say(_loc_2);
            return;
        }// end function

        public function setAddress(param1:String) : void
        {
            SWFAddress.setValue(param1);
            this.execute();
            this.say("Cambiando de secci?n " + param1);
            return;
        }// end function

        private function execute() : void
        {
            var _loc_1:* = SWFAddress.getPath();
            var _loc_2:* = _loc_1.split("/");
            var _loc_3:uint;
            var _loc_4:uint;
            while (_loc_4++ < _loc_2.length)
            {
                // label
                if (_loc_2[_loc_4] != "")
                {
                    Flow.level("level" + _loc_3).request(_loc_2[_loc_4], "SWFAddressProxy");
                }// end if
            }// end while
            return;
        }// end function

        public function get home() : Boolean
        {
            this.say("chequeando home : " + SWFAddress.getPath());
            return SWFAddress.getPath() == "/";
        }// end function

        public static function getInstance() : SWFAddressProxy
        {
            instance = instance ? (instance) : (new SWFAddressProxy);
            return instance;
        }// end function

    }
}
